package resilienceManager.qmul.gemom;

import inl.resilienceManager.qmul.gemom.VarianceCalculation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;

import javax.swing.tree.DefaultMutableTreeNode;


/**
 * The node used for the bid tree
 * 
 * extends DefaultMutableTreeNode
 * 
* @author Peng Jiang, John Bigham, Jiayi Wu ------------QMUL
 * @version 0.1 
 * 
 * 
 * */

public class bidNode extends DefaultMutableTreeNode {
	
//	items no.
	private LinkedList<Integer> items_;
	private Gbroker bidder = new Gbroker(99, 99);
	
	
	public bidNode(String s,LinkedList<Integer> item )
	{
		super(s);
		
		items_ = item;
	}
	
	public bidNode(String s,LinkedList<Integer> item , Gbroker bidder)
	{
		super(s);
		
		items_ = item;
		this.bidder = bidder;
	}
	
	public bidNode(String s)
	{
		super(s);
	}
	
	
	public LinkedList<Integer> getBidderItemIDs(){
		return bidder.getItemIDs();
	}
	
	public ArrayList<NSitem> getBidderItems(){
		return bidder.getCurrentItems();
	}
	
	public Gbroker getBidder(){
		return bidder;
	}
	
	public LinkedList<Integer> getBidItem()
	{
		return items_;
	}
	
//	public LinkedList<Integer> getBidderItems(){
//		bidder.getCurrentItems();
//	}
//	TODO done? change size() to mean value!/ add current bidder items 
	public int getNodevalue(NSitem[] itemlist)
	{
//		no. of namespaces
		int totalValue=0;
		for(int i=0; i<items_.size(); i++)
		{
			int itemID=items_.get(i);
			for(NSitem ni :itemlist){
				if (ni.getID()==itemID){
					totalValue=totalValue+ni.getNSlist().size();
//					items[i]=ni;
				}
			}

			
		}
		
//		FIXME itemlist is bid items; do not the bidder's current items 
		if (getBidderItems()!=null && getBidderItems().size()>0){
			for (NSitem i: getBidderItems()){
				totalValue=totalValue+i.getNSlist().size();
			}
		}
		
		return totalValue;
	}
	
	
//	TODO add current bidder items
	public double getNodeVariance(NSitem[] itemlist)
	{
		double v=0;
		
		NSitem[] items= new NSitem[items_.size()];
//		
		try{
		
		for(int i=0; i<items_.size(); i++)
		{
			int itemID=items_.get(i);
//			itemslist
			for(NSitem ni :itemlist){
				if (ni.getID()==itemID){
					items[i]=ni;
				}
			}
//			items[i]=itemlist[itemID];
			
		}
		} catch (Exception e){
			System.out.println("items_.size()"+items_.size());
			System.out.println("itemlist.length"+itemlist.length);
			System.out.println();
			System.out.println();
		}
		
		
		
		if (getBidderItems()!=null && getBidderItems().size()>0){
			ArrayList<NSitem> al = new ArrayList<NSitem>();
			al.addAll(getBidderItems());
			al.addAll(Arrays.asList(items));
			items= al.toArray(new NSitem[al.size()]);
		}
		
		v= VarianceCalculation.sumItem(items);
		
		return v;
		
	}
	
	public double getNodeMean(NSitem[] itemlist)
	{
		double mean=0;
		
		NSitem[] items= new NSitem[items_.size()];
//		
		try{
			
			for(int i=0; i<items_.size(); i++)
			{
				int itemID=items_.get(i);
//			itemslist
				for(NSitem ni :itemlist){
					if (ni.getID()==itemID){
						items[i]=ni;
					}
				}
//			items[i]=itemlist[itemID];
				
			}
		} catch (Exception e){
			System.out.println("items_.size()"+items_.size());
			System.out.println("itemlist.length"+itemlist.length);
			System.out.println();
			System.out.println();
		}
		
		
		
		if (getBidderItems()!=null && getBidderItems().size()>0){
			ArrayList<NSitem> al = new ArrayList<NSitem>();
			al.addAll(getBidderItems());
			al.addAll(Arrays.asList(items));
			items= al.toArray(new NSitem[al.size()]);
		}
//		all items including new mirror and new primary
//		v= VarianceCalculation.sumItem(items);
		mean=VarianceCalculation.sumMean(items);
		
		return mean;
		
	}

}
